#! /usr/bin/env python

import os, sys
import pygame
from pygame.locals import *
from helpers import *

class Title:

    def __init__(self, screen):
        self.screen = screen

    def start(self):

        self.background = pygame.Surface(self.screen.get_size())
        self.background = self.background.convert()
        self.background.fill((0,0,0))
        self.screen.blit(self.background, (0,0))

        self.selected = 0
        self.items = self.load_menuitems(("Start Game", "Help", "Quit"))
        self.item_sprites = pygame.sprite.Group(self.items)

        self.print_menu()
        pygame.display.flip()

        while 1:
            event = pygame.event.wait()
            if event.type == KEYDOWN:
                if event.key == pygame.K_UP:
                    self.process_arrow(True)
                elif event.key == pygame.K_DOWN:
                    self.process_arrow(False)
                elif event.key == pygame.K_RETURN:
                    return self.selected
                elif event.key == pygame.K_ESCAPE:
                    return 2

            self.print_menu()
            pygame.display.flip()

    def load_menuitems(self, names):

        font = pygame.font.Font(None, 48)
        font.set_bold(True)
        spacer = font.render("M", 1, (0,255,0))

        centery = self.screen.get_rect().centery
        font_height = spacer.get_rect().height
        starty = centery - (((2 * len(names)) - 1) * font_height) / 2
        items = []
        for name in names:
            item = MenuItem(name, starty, self.screen.get_rect().centerx)
            items.append(item)
            starty = starty + (font_height * 2)

        return items

    def process_arrow(self, up):
        if up:
            self.selected = self.selected - 1
        else:
            self.selected = self.selected + 1

        if self.selected < 0:
            self.selected = len(self.items) - 1
        elif self.selected > len(self.items) - 1:
            self.selected = 0

    def print_menu(self):
        srec = self.screen.get_rect()
        self.screen.blit(self.background, (0,0))
        title_font = pygame.font.Font(None, 78)
        title_font.set_bold(True)
        title = title_font.render("Photo Detective", 1, (255, 0, 0))
        titlex = srec.centerx - title.get_rect().width / 2
        titley = (title.get_rect().height * 1)
        title_height = title.get_rect().height
        self.screen.set_clip(Rect(titlex, titley, srec.right, title_height))
        self.screen.blit(title, (titlex, titley))

        for x in range(len(self.items)):
            self.items[x].set_selected(x == self.selected)

        self.screen.set_clip(self.screen.get_rect())
        self.item_sprites.draw(self.screen)

class MenuItem(pygame.sprite.Sprite):

    def __init__(self, text, starty, centerx):
        pygame.sprite.Sprite.__init__(self)
        font = pygame.font.Font(None, 48)
        self.item = font.render(text, 1, (0,255,0))
        font.set_bold(True)
        self.item_selected = font.render(text, 1, (0,255,0))
        selrect = self.item_selected.get_rect()
        unselrect = self.item.get_rect()
        selrect.top = starty
        unselrect.top = starty
        selrect.left = centerx - selrect.width / 2
        unselrect.left = centerx - unselrect.width / 2
        self.item_selected_rect = selrect
        self.item_rect = unselrect
        self.set_selected(False)

    def toggle_selected(self,force=None):
        if force != None:
            self.set_selected(force)
        else:
            self.set_selected(not(self.selected))

    def set_selected(self,val):
        self.selected = val
        if val:
            self.image = self.item_selected
            self.rect = self.item_selected_rect
        else:
            self.image = self.item
            self.rect = self.item_rect
